package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.service.impl.BaseServiceImpl;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.DictionariesDao;
import com.djinfo.work.dao.impl.DictionariesDaoImpl;
import com.djinfo.work.entity.DicVo;
import com.djinfo.work.entity.Dictionaries;
import com.djinfo.work.service.DictionariesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * Created by admin on 2017/5/19.
 */
@Service("DictionariesService")
public class DictionariesServiceImpl extends BaseServiceImpl<Dictionaries,DictionariesDaoImpl> implements DictionariesService {
    @Autowired
    private DictionariesDao dictionariesDao;

    @Override
    public Dictionaries getDepartmentById(String id) {
        return dictionariesDao.getDictionariesById(id);
    }

    @Override
    public List<Dictionaries> page(Dictionaries object, Integer pageNum, Integer pageSize) {
        return dictionariesDao.page(object,pageNum,pageSize);
    }

    @Override
    public List<Dictionaries> list() {
        return dictionariesDao.list();
    }

    @Override
    public Integer getAllRowCount(Dictionaries dictionaries) {
        return dictionariesDao.getAllRowCount(dictionaries);
    }

    @Override
    public List<Dictionaries> messagesTypeList(String type) {
        return dictionariesDao.messagesTypeList(type);
    }

    @Override
    public List<Dictionaries> messagesStatusList(String type) {
        return dictionariesDao.messagesStatusList(type);
    }

    @Override
    public List<DicVo> getDic(String dic) {
        return dictionariesDao.getDic(dic);
    }

    @Override
    public AjaxJson list(Dictionaries dictionaries, Integer pagenum, Integer pagesize) {
        AjaxJson ajaxJson = null;
        try {
            HashMap<String, Object> map = new HashMap<>();
            ajaxJson = new AjaxJson();
            List list=dictionariesDao.page(dictionaries,pagenum,pagesize);
            Integer count = dictionariesDao.getAllRowCount(dictionaries);
            map.put("dataList",list);
            map.put("total",count);
            ajaxJson.setResult(map);
        } catch (Exception e) {
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    @Override
    public AjaxJson saveOrUpdate(Dictionaries dictionaries, HttpSession session) {
        AjaxJson ajaxJson = null;
        try {
            ajaxJson = new AjaxJson();
            if(dictionaries.getId()==null||dictionaries.getId().equals("")){
                dictionaries.setId(null);
                String nowTime = StringUtil.getNowTime();
                dictionaries.setCreateTime(nowTime);
                dictionaries.setUpdateTime(nowTime);
                dictionariesDao.save(dictionaries);
            }else {
                String nowTime = StringUtil.getNowTime();
                dictionaries.setUpdateTime(nowTime);
                dictionariesDao.update(dictionaries);
            }
        } catch (Exception e) {
            ajaxJson.setCode(301);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }

        return ajaxJson;
    }

    @Override
    public AjaxJson deleteDictionary(Dictionaries dictionaries) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            dictionariesDao.delete(dictionaries);
        } catch (Exception e) {
            ajaxJson.setCode(301);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }
}
